﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace yncTech.Common
{
    public class ExcelExporter
    {
        private DataSet _dataSet = null;
        /// <summary> 
        /// 要导出的DataSet 
        /// </summary> 
        public DataSet DataSet
        {
            get { return _dataSet; }
        }
        /// <summary> 
        /// 构造一个ExcelExporter 
        /// </summary> 
        public ExcelExporter()
        {
            _dataSet = new DataSet();
        }
        /// <summary> 
        /// 构造一个ExcelExporter 
        /// </summary> 
        /// <param name="dataSet">要导出的DataSet</param> 
        public ExcelExporter(DataSet dataSet)
        {
            _dataSet = dataSet;
        }


        public void DownloadExcel()
        {
        }
        public void ExportToExcel(DataTable data,HttpResponse response,string fileName)
        {
            StringBuilder tempBuilder = new StringBuilder();
            tempBuilder.AppendLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
            tempBuilder.AppendLine("<head>");
            tempBuilder.AppendLine("<!--[if gte mso 9]>");
            tempBuilder.AppendLine("<xml>");
            tempBuilder.AppendLine(" <x:ExcelWorkbook>");
            tempBuilder.AppendLine("  <x:ExcelWorksheets>");
            tempBuilder.AppendLine("   <x:ExcelWorksheet>");
            tempBuilder.AppendLine("    <x:Name>Sheet1</x:Name>");
            tempBuilder.AppendLine("    <x:WorksheetOptions>");
            tempBuilder.AppendLine("      <x:Print>");
            tempBuilder.AppendLine("       <x:ValidPrinterInfo />");
            tempBuilder.AppendLine("      </x:Print>");
            tempBuilder.AppendLine("    </x:WorksheetOptions>");
            tempBuilder.AppendLine("   </x:ExcelWorksheet>");
            tempBuilder.AppendLine("  </x:ExcelWorksheets>");
            tempBuilder.AppendLine("</x:ExcelWorkbook>");
            tempBuilder.AppendLine("</xml>");
            tempBuilder.AppendLine("<![endif]-->");
            tempBuilder.AppendLine("</head>");
            tempBuilder.AppendLine("<body>");
            tempBuilder.AppendLine(DataTableToHtml(data));
            tempBuilder.AppendLine("</table>");
            tempBuilder.AppendLine("</body>");
            tempBuilder.AppendLine("</html>");
            

            response.Clear();
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.Charset = "UTF-8";
            response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls",System.Web.HttpUtility.UrlEncode(fileName)));
            response.Cache.SetNoStore();//history back 不重复
            response.ClearContent();
            response.ContentType = "application/x-xls";
            response.Write("<meta http-equiv=Content-Type content=\"ms-excel; charset=utf-8\">");
            response.Write(tempBuilder);
            response.End();
        }

        string DataTableToHtml(DataTable table)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table cellpadding='0' style='margin-top: 20px; border: 1px solid black; border-collapse: collapse; font: normal 10pt Arial; ' cellspacing='0'>");
            sb.AppendLine("<tr>");
            for (int i = 0; i < table.Columns.Count; i++)
            {
                sb.AppendLine(string.Format("<td>{0}</td>", table.Columns[i].ColumnName));
            }
            sb.AppendLine("</tr>");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                sb.AppendLine("<tr>");
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    sb.AppendLine(string.Format("<td>{0}</td>", table.Rows[i][j].ToString()));
                }
                sb.AppendLine("</tr>");
            }
            return sb.ToString();

        }

    }
}
